﻿using System;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ZJGIS.ArcGIS.Custom;


namespace ZJGIS.ManageUser
{
    /// <summary>
    /// 作用：节点序列化
    /// 作者：盛琦
    /// 修改日期：2009-06-19
    /// </summary>
    public static class NodeSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static IResourceNode FromXml(string xml)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNode xmlNode = xmlDoc.DocumentElement;
            return FromXmlNode(xmlNode);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ConstructUserResourceXml()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
            xml += "<Resource/>";
            return xml;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <returns></returns>
        public static IResourceNode FromXmlNode(XmlNode xmlNode)
        {
            try
            {
                string name = xmlNode.Name;
                IResourceNode node = null;
                if (name.Equals("Group"))
                {
                    GroupNode gNode = new GroupNode();
                    gNode.Name = xmlNode.Attributes["name"].Value;
                    node = gNode;
                }
                else if (name.Equals("FeatureClass"))
                {
                    GeoDataNode fcNode = new GeoDataNode();
                    fcNode.Name = xmlNode.Attributes["name"].Value;
                    fcNode.TypeName = xmlNode.Attributes["typename"].Value;
                    Type pType = Type.GetType(fcNode.TypeName);
                    if (pType != null)
                    {
                        Object obj = Activator.CreateInstance(pType);

                        if (obj is IDataNode)
                        {
                            IDataNode dnode = obj as IDataNode;
                            dnode.Name = xmlNode.Attributes["value"].Value;
                            string propertyset = xmlNode.Attributes["dbconnection"].Value;
                            WORKSPACETYPE workspacetype = (WORKSPACETYPE)int.Parse(xmlNode.Attributes["workspacetype"].Value);
                            XmlNode ceNode = xmlNode.Attributes["canedit"];
                            if (ceNode != null)
                            {
                                fcNode.Canedit = bool.Parse(ceNode.Value);
                            }
                            dnode.WorkspaceType = workspacetype;
                            IPropertySet pPropertySet = new PropertySetClass();
                            string[] strAllItems = propertyset.Split(';');
                            foreach (string item in strAllItems)
                            {
                                string[] strItems = item.Split('=');
                                if (strItems[0].ToUpper() == "SERVER")
                                {
                                    pPropertySet.SetProperty("SERVER", strItems[1]);
                                }
                                if (strItems[0].ToUpper() == "INSTANCE")
                                {
                                    pPropertySet.SetProperty("INSTANCE", strItems[1]);
                                }
                                if (strItems[0].ToUpper() == "USER")
                                {
                                    pPropertySet.SetProperty("USER", strItems[1]);
                                }
                                if (strItems[0].ToUpper() == "VERSION")
                                {
                                    pPropertySet.SetProperty("VERSION", strItems[1]);
                                }
                                if (strItems[0].ToUpper() == "AUTHENTICATION_MODE")
                                {
                                    pPropertySet.SetProperty("AUTHENTICATION_MODE", strItems[1]);
                                }
                                if (strItems[0].ToUpper() == "PASSWORD")
                                {
                                    string strPassword = strItems[1];
                                    if (strPassword.Contains("|"))
                                    {
                                        string[] strPasswords = strPassword.Split('|');
                                        byte[] bytePassword = new byte[strPasswords.Length];
                                        for (int j = 0; j < strPasswords.Length; j++)
                                        {
                                            bytePassword[j] = Convert.ToByte(strPasswords[j]);
                                        }
                                        pPropertySet.SetProperty("PASSWORD", bytePassword);
                                    }
                                    else
                                    {
                                        pPropertySet.SetProperty("PASSWORD", strPassword);
                                    }
                                }
                                if(strItems[0].ToUpper()=="DATABASE")
                                {
                                    pPropertySet.SetProperty("DATABASE", strItems[1]);
                                }
                            }
                            dnode.PropertySet = pPropertySet;
                            fcNode.DataNode = dnode;
                            node = fcNode;
                        }
                    }
                }
                else if (name.Equals("D4"))
                {
                    D4Node d4Node = new D4Node();
                    d4Node.Name = xmlNode.Attributes["name"].Value;
                    d4Node.D4Sourse = xmlNode.Attributes["d4Sourse"].Value;
                    d4Node.Password = xmlNode.Attributes["password"].Value;
                    d4Node.Username = xmlNode.Attributes["username"].Value;
                    ProcessStartInfo info = new ProcessStartInfo("net", "use " + d4Node.D4Sourse + " " + d4Node.Password + " /USER:" + d4Node.Username);
                    info.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(info);
                    node = d4Node;
                }
                else if (name.Equals("Project"))
                {
                    ProjectNode pNode = new ProjectNode();
                    pNode.Name = xmlNode.Attributes["name"].Value;
                    pNode.Value = xmlNode.Attributes["value"].Value;
                    node = pNode;
                }
                else if (name.Equals("UserDataGroup"))
                {
                    UserDataGroup udNode = new UserDataGroup();
                    udNode.Name = xmlNode.Attributes["name"].Value;
                    node = udNode;
                }
                else if (name.Equals("UserProjectGroup"))
                {
                    UserProjectGroup upNode = new UserProjectGroup();
                    upNode.Name = xmlNode.Attributes["name"].Value;
                    node = upNode;
                }
                else if (name.Equals("FileNode"))
                {
                    FileNode fNode = new FileNode();
                    fNode.Name = xmlNode.Attributes["name"].Value;
                    fNode.Value = xmlNode.Attributes["value"].Value;
                    node = fNode;
                }
                if (node != null)
                {
                    if (xmlNode.Attributes["enable"] != null)
                    {
                        bool enable = false;
                        bool.TryParse(xmlNode.Attributes["enable"].Value, out enable);
                        node.Enable = enable;
                    }
                }
                foreach (XmlNode xmlNode1 in xmlNode.ChildNodes)
                {
                    if (node != null)
                    {
                        IResourceNode tNode = FromXmlNode(xmlNode1);
                        if (tNode != null)
                            node.ChildNodes.Add(tNode);
                    }

                }
                return node;
            }
            catch (Exception ex)
            {
                
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="resourceNode"></param>
        /// <returns></returns>
        public static XmlDocument ToXml(UserResource resourceNode)
        {
            XmlDocument xmlDoc = new XmlDocument();
            resourceNode.ToXmlNode(xmlDoc);
            return xmlDoc;
        }
    }
}
